package com.youlu.campus.service.activity;

import com.youlu.campus.entity.PointPlatformTaskConfigQueryVO;
import com.youlu.campus.entity.SubTemplateSceneConfig;
import com.youlu.campus.entity.enums.TaskBizType;
import com.youlu.campus.entity.platform.PlatformTaskInfo;
import com.youlu.campus.service.platform.PointPlatformTaskConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class SubTemplateSceneConfigService {

    @Autowired
    private PointPlatformTaskConfigService pointPlatformTaskConfigService;


    @Resource(name = "masterMongoTemplate")
    private MongoTemplate masterMongoTemplate;

    public SubTemplateSceneConfig findByTemplateIdAndActivityId(String appId, String platform,
                                                                TaskBizType bizType, String activityId) {
        if (StringUtils.isBlank(appId) || StringUtils.isBlank(platform) || Objects.isNull(bizType)) {
            return null;
        }
        log.info("请求参数 appId->{}|platform ->{}| bizType",appId,platform,bizType);
        PointPlatformTaskConfigQueryVO pointPlatformTaskConfigQueryVO = new PointPlatformTaskConfigQueryVO();
        pointPlatformTaskConfigQueryVO.setPlatform(platform);
        pointPlatformTaskConfigQueryVO.setAppId(appId);
        pointPlatformTaskConfigQueryVO.setStatus(1);
        pointPlatformTaskConfigQueryVO.setTaskBizType(bizType);
        PlatformTaskInfo platformTaskInfo =
                pointPlatformTaskConfigService.findTmplByAppId(pointPlatformTaskConfigQueryVO);
        if (Objects.isNull(platformTaskInfo)) {
            return null;
        }
        log.info("请求参数 appId->{}|platform ->{}| bizType->{}",appId,platform,bizType);
        List<String> taskExtra = platformTaskInfo.getTaskExtra();
        if (CollectionUtils.isEmpty(taskExtra)) {
            return null;
        }
        String templateId = taskExtra.get(0);
        Query query = new Query();
        query.addCriteria(Criteria.where("templateId").is(templateId));
        query.addCriteria(Criteria.where("activityId").is(activityId));
        SubTemplateSceneConfig subTemplateSceneConfig = this.masterMongoTemplate.findOne(query,
                SubTemplateSceneConfig.class);
        if (Objects.nonNull(subTemplateSceneConfig)) {
            return subTemplateSceneConfig;
        }
        query = new Query();
        query.addCriteria(Criteria.where("templateId").is(templateId));
        query.with(Sort.by(Sort.Direction.DESC, "id"));
        subTemplateSceneConfig = this.masterMongoTemplate.findOne(query, SubTemplateSceneConfig.class);
        query = new Query();
        query.addCriteria(Criteria.where("templateId").is(templateId));
        query.addCriteria(Criteria.where("activityId").is(activityId));
        Update update = new Update();
        if (Objects.isNull(subTemplateSceneConfig)) {
            update.inc("scene", 1);
        } else {
            update.inc("scene", subTemplateSceneConfig.getScene() + 1);
        }
        Date now = new Date();
        update.set("updatedTime", now);
        update.setOnInsert("createdTime", now);
        FindAndModifyOptions findAndModifyOptions = new FindAndModifyOptions();
        findAndModifyOptions.returnNew(Boolean.TRUE);
        findAndModifyOptions.upsert(Boolean.TRUE);
        return this.masterMongoTemplate.findAndModify(query, update, findAndModifyOptions, SubTemplateSceneConfig.class);
    }
}
